##############################################################################

library(data.table)
library(argparser)

##############################################################################

argp <- arg_parser("Plot the deconstructSigs")
argp <- add_argument(argp, "--input_file" , help="")
argp <- add_argument(argp, "--baseline_file" , help="")
argp <- add_argument(argp, "--out_file" , help="")


argv <- parse_args(argp)

out_file <- argv$out_file
baseline_file <- argv$baseline_file
input_file <- argv$input_file


if(1!=1){

	input_file <- "/public/home/xxf2019/20220205_lungSomatic/results/vcf_qc/Record_All.MutQcNum.csv"
	baseline_file <- "/public/home/xxf2019/20220205_lungSomatic/config/NJMU_HUAXI-useCombine_v6.txt"
	out_file <- "/public/home/xxf2019/20220205_lungSomatic/results/vcf_qc/Record_All.MutQcNum.rate.csv"
}

##############################################################################
dat <- data.frame(fread(input_file , header = T))
dat_baseline <- data.frame(fread(baseline_file , header = T))
dat_baseline <- data.frame(TumorID = dat_baseline$TumorID , histology = dat_baseline$histology)

##############################################################################
## 标注比例
for( col in colnames(dat)[4:22] ){
	new_name <- paste0( col , "_rate" )
	dat[,new_name] <- dat[,col]/dat$Raw_muts
}


##############################################################################
## 列名的顺序
col_order <- c()
for( i in 4:22){
	col_order <- c( col_order , colnames(dat)[i] , colnames(dat)[i+19] )
}

result <- dat[,c("Tumor" , "Normal" , "Raw_muts" , col_order)]

result <- merge( dat_baseline , result , by.x = "TumorID" , by.y = "Tumor" )

write.csv(result , out_file , row.names = F , quote = F)